<template>
  <div class="app-container">
    <div class="filter-container">
      <el-row type="flex" class="row-bg" justify="space-between">
        <el-col :span="24">
          <el-col :span="5" style=" margin-right: 10px">
            <el-input
              v-model="queryParam.traceId"
              placeholder="traceId"
              class="input-with-select"
              maxlength="16"
              clearable
              show-word-limit
              @keyup.enter.native="handleQuery"
            >
              <el-button slot="append" icon="el-icon-search" @keyup.enter.native="handleQuery" @click="handleQuery"/>
            </el-input>
          </el-col>
          <el-button-group>
            <el-button
              v-permission="['ROLE_ADMIN', 'role_list_delete']"
              :loading="btnDeleteLoading"
              type="primary"
              icon="el-icon-delete"
              @click="handleBatchDelete"
            >
              删除
            </el-button>
          </el-button-group>
        </el-col>
      </el-row>
    </div>
    <el-table
      :loading="listLoading"
      :data="list"
      :header-cell-style="{'text-align':'center'}"
      element-loading-text="Loading"
      border
      fit
      highlight-current-row
      @selection-change="handleSelected"
    >
      <el-table-column type="selection" width="50" align="center"/>
      <el-table-column width="50" type="index" label="-" align="center"/>
      <el-table-column width="200" label="TraceId" prop="traceId"/>
      <el-table-column width="120" label="上报人" prop="createUserNickname"/>
      <el-table-column width="155" label="上报时间" prop="createTimeFormat"/>
      <el-table-column label="日志内容" prop="message">
        <template v-slot="{row}">{{ row.truncateMessage() }}</template>
      </el-table-column>
      <el-table-column fixed="right" label="操作" width="140" align="center">
        <template v-slot="{row, $index}">
          <el-tooltip content="详情" placement="top" :disabled="$index!==0">
            <el-button size="mini" icon="el-icon-document" circle @click="handleDetail(row, $index)"/>
          </el-tooltip>
          <el-tooltip content="删除" placement="top" :disabled="$index!==0">
            <el-popconfirm title="是否确认删除？" placement="top" @confirm="handleDelete(row, $index)">
              <el-button
                slot="reference"
                v-permission="['ROLE_ADMIN', 'role_list_delete']"
                size="mini"
                type="danger"
                icon="el-icon-delete"
                circle
                style="margin-left: 10px"
              />
            </el-popconfirm>
          </el-tooltip>
        </template>
      </el-table-column>
    </el-table>
    <div class="pagination-container">
      <el-pagination
        background
        :current-page.sync="queryParam.page.number"
        :page-size.sync="queryParam.page.limit"
        :total.sync="queryParam.page.totalCount"
        :page-sizes="[10, 20, 50, 100]"
        layout="total, sizes, prev, pager, next, jumper"
        @size-change="handleQuery"
        @current-change="handleQuery"
      />
    </div>

    <error-log-detail-drawer v-if="visibleDetailDrawer" :row="selectedRow" :visible.sync="visibleDetailDrawer"/>
  </div>
</template>

<script>
import ErrorLog from '@/api/entity/ErrorLog'
import Page from '@/utils/entity/Page'
import permission from '@/directive/permission'
import ErrorLogDetailDrawer from './components/ErrorLogDetailDrawer'
import ErrorLogService from '@/api/ErrorLogService'
import OrderBy from '@/utils/entity/OrderBy'

export default {
  components: {ErrorLogDetailDrawer},
  directives: {permission},
  data() {
    return {
      queryParam: new ErrorLog({
        page: Page.ofDefault(),
        orderBy: [OrderBy.desc('id')]
      }),
      list: null,
      listLoading: true,
      btnDeleteLoading: false,
      selectedRows: [],
      selectedRow: new ErrorLog(),
      visibleDetailDrawer: false,
      visibleSaveDrawer: false
    }
  },
  created() {
    this.handleQuery()
  },
  methods: {
    async handleQuery() {
      this.listLoading = true
      const result = await ErrorLogService.pageable(this.queryParam).finally(() => (this.listLoading = false))
      result
        .ifToastErrorMessage('列表查询失败')
        .syncPageCount(this.queryParam.page)
        .ifSuccess(() => {
          this.list = result.data.map(row => {
            row.visible = false
            return row
          })
        })
    },
    /**
     *
     * @param rows {ErrorLog}
     */
    handleSelected(rows) {
      this.selectedRows = rows
    },
    /**
     *
     * @param row {ErrorLog}
     */
    handleDetail(row) {
      this.selectedRow = new ErrorLog(row)
      this.visibleDetailDrawer = true
    },
    /**
     *
     * @param row {ErrorLog}
     */
    async handleDelete(row) {
      this.listLoading = true
      const result = await ErrorLogService.markDeleteById(row.dv).finally(_ => (this.listLoading = false))
      result
        .ifToastMessage('删除成功', '删除失败')
        .ifSuccess(() => {
          this.handleQuery()
        })
    },
    handleBatchDelete() {
      if (this.selectedRows.length === 0) {
        this.$message.warning('请至少选择一行数据')
        return
      }
      this.$confirm('是否确认删除选中数据?', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        this.btnDeleteLoading = true
        const result = await ErrorLogService.markDeleteByIds(this.selectedRows.map(row => row.dv)).finally(() => (this.btnDeleteLoading = false))
        result
          .ifToastMessage('删除成功', '删除失败')
          .ifSuccess(() => {
            this.handleQuery()
          })
      })
    }
  }
}
</script>
